package demoMod.nanaki.patches;

import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import demoMod.nanaki.cards.tempCards.AbstractBlessingCard;
import demoMod.nanaki.interfaces.OnMoveToTopOfDeckSubscriber;

import java.util.ArrayList;
import java.util.stream.Collectors;

public class CardGroupPatch {
    @SpirePatch(
            clz = CardGroup.class,
            method = "moveToDeck"
    )
    public static class PatchmoveToDeck {
        public static void Prefix(CardGroup cgp, AbstractCard c, boolean randomSpot) {
            if (randomSpot && !cgp.isEmpty()) return;
            if (c instanceof OnMoveToTopOfDeckSubscriber) {
                ((OnMoveToTopOfDeckSubscriber) c).onMoveToTopOfDeck();
            }
        }
    }

    @SpirePatch(
            clz = CardGroup.class,
            method = "moveToBottomOfDeck"
    )
    public static class PatchmoveToBottomOfDeck {
        public static void Prefix(CardGroup cgp, AbstractCard c) {
            if (cgp.isEmpty() && c instanceof OnMoveToTopOfDeckSubscriber) {
                ((OnMoveToTopOfDeckSubscriber) c).onMoveToTopOfDeck();
            }
        }
    }

    @SpirePatch(
            clz = CardGroup.class,
            method = "getSkills"
    )
    public static class PatchGetSkills {
        public static SpireReturn<CardGroup> Prefix(CardGroup group) {
            CardGroup ret = new CardGroup(CardGroup.CardGroupType.UNSPECIFIED);
            if (group.group != null) {
                ret.group = group.group.stream().filter(c -> c.type == AbstractCard.CardType.SKILL && !(c instanceof AbstractBlessingCard)).collect(Collectors.toCollection(ArrayList::new));
            } else {
                ret.group = new ArrayList<>();
            }
            return SpireReturn.Return(ret);
        }
    }
}
